const fs = require('fs-extra');
const path = require('path');

class ProjectBuilder {
  constructor(targetPath) {
    this.targetPath = targetPath;
  }

  async buildProject(templates, generatedCode) {
    try {
      // 确保目标目录存在
      await fs.ensureDir(this.targetPath);
      console.log('Created target directory:', this.targetPath);

      // 为每个模板创建对应的文件
      for (const [pageName, template] of Object.entries(templates)) {
        const pageDir = path.join(this.targetPath, pageName);
        await fs.ensureDir(pageDir);
        
        const filePath = path.join(pageDir, 'index.ux');
        const content = generatedCode[pageName];
        
        if (!content) {
          console.warn(`No content generated for ${pageName}, skipping...`);
          continue;
        }
        
        await fs.writeFile(filePath, content, 'utf8');
        console.log(`Generated file: ${filePath}`);
      }
    } catch (error) {
      console.error('构建项目失败:', error);
      throw error;
    }
  }
}

module.exports = ProjectBuilder; 